package com.cf.user.login;

import com.cf.expire.DefaultExpireService;
import com.cf.expire.ExpireService;
import com.cf.user.core.part.wx.WxUserService;
import com.cf.user.login.controller.LoginController;
import com.cf.user.login.filter.LoginFilter;
import com.cf.user.login.filter.RequestAwareFilter;
import com.cf.user.login.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * game-server-UserSysConfig
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/28 下午 07:20
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(UserSystemProperty.class)
public class UserSysConfig extends WebMvcConfigurerAdapter {


    @Bean
    @ConditionalOnMissingBean(TokenManager.class)
    public TokenManager tokenManager() {
        log.info("未找到自定义的TokenManager，实例化默认的DefaultTokenManagerImpl");
        return new DefaultTokenManagerImpl();
    }

    //#####################用户########################################
    @Bean
    @ConditionalOnMissingBean(UserInfoService.class)
    public UserInfoService userInfoService() {
        log.info("未找到自定义的UserInfoService，实例化默认的DefaultUserInfoService");
        return new UserInfoServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(UserPartService.class)
    public UserPartService userPartService() {
        log.info("未找到自定义的UserPartService，实例化默认的ThirdPartUserServiceImpl");
        return new ThirdPartUserServiceImpl();
    }

    //########################第三方登陆##########################
    @Bean
    @ConditionalOnMissingBean(WxUserService.class)
    public WxUserService wxUserService() {
        log.info("未找到自定义的WxUserService，实例化默认的WxUserService");
        return new WxUserService();
    }

    @Bean
    @ConditionalOnMissingBean(ExpireService.class)
    public ExpireService expireService() {
        log.info("未找到自定义的ExpireService，实例化默认的DefaultExpireService");
        return new DefaultExpireService();
    }


    //########################controller###############################
    @Bean
    @ConditionalOnMissingBean(LoginController.class)
    public LoginController loginController() {
        log.info("未找到自定义的LoginController，实例化默认的LoginController");
        return new LoginController();
    }


    //########################inceptrer###############################

    @Bean
    public LoginFilter loginFilter() {
        log.info("实例化LoginFilter");
        return new LoginFilter();
    }

    @Bean
    public FilterRegistrationBean loginFilterRegistrationBean(LoginFilter loginFilter) {
        log.info("注册LoginFilter");
        FilterRegistrationBean myFilter = new FilterRegistrationBean();
        myFilter.addUrlPatterns(loginFilter.matchPathPatterns().toArray(new String[]{}));
        myFilter.setOrder(myFilter.getOrder());
        myFilter.setFilter(loginFilter);
        return myFilter;
    }

    @Bean
    public RequestAwareFilter requestAwareFilter() {
        log.info("实例化RequestAwareFilter");
        return new RequestAwareFilter();
    }

    @Bean
    public FilterRegistrationBean requestAwareFilterRegistrationBean(RequestAwareFilter requestAwareFilter) {
        log.info("注册RequestAwareFilter");
        FilterRegistrationBean myFilter = new FilterRegistrationBean();
        myFilter.addUrlPatterns("/*");
        myFilter.setOrder(-100);
        myFilter.setFilter(requestAwareFilter);
        return myFilter;
    }


}
